package com.ys.controller.web.storage;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.storage.TccPreAllocatePublish;
import com.ys.service.storage.TccPreAllocatePublishService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/web/storage/tccPreAllocatePublish")
public class WebTccPreAllocatePublishController {
	
	@Autowired
	private CommonApiService<TccPreAllocatePublish> commonApiService;
	@Autowired
	private TccPreAllocatePublishService tccPreAllocatePublishService;

	private final Long MENU_ID = MenuId.TCC_PreAllocatePublish;


	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response){

		try {
			String bid = commonApiService.getCommonNextBid(MENU_ID);

			TccPreAllocatePublish entity = new TccPreAllocatePublish()
					.setBid(bid)
					.setPublishAmount(BigDecimal.ZERO)
					.setPublishQuantity(BigDecimal.ZERO)
					.setCreateTime(new Date());
			
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MENU_ID, new ArrayList<Map<String, Object>>());
			
			return commonApiService.toAdd(model, entity, MENU_ID, null, subGridConfig);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	
	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,
			String bid){
		if (bid == null || bid.length() == 0) {
			return W.error(model, "单号不能为空");
		}

		try {
			Map<String,Object> extraParam = tccPreAllocatePublishService.getExtraParam(bid);

			// 子列表配置信息
			List<Map<String, Object>> sonlist = tccPreAllocatePublishService.getItemListByBid(bid);
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MENU_ID, sonlist);
			
			return commonApiService.toEdit(model, Long.parseLong(extraParam.get("id").toString()), new TccPreAllocatePublish(), MENU_ID, extraParam, subGridConfig);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}



	/*
	 * 详情
	 */
	@GetMapping("/toDetail")
	public String toDetail(Model model, HttpServletRequest request, HttpServletResponse response,
						 String bid){
		if (bid == null || bid.length() == 0) {
			return W.error(model, "单号不能为空");
		}

		try {
			Map<String,Object> extraParam = tccPreAllocatePublishService.getExtraParam(bid);

			// 子列表配置信息
			List<Map<String, Object>> sonlist = tccPreAllocatePublishService.getItemListByBid(bid);
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MENU_ID, sonlist);
			subGridConfig.setButtonArr(new ArrayList<>());

			return commonApiService.toDetail(model, Long.parseLong(extraParam.get("id").toString()), new TccPreAllocatePublish(), MENU_ID, extraParam, subGridConfig);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}

}